const app = getApp()

Page({

  data: {
    showbar:true,
    info:{
      is_vip:0,
      info:{}
    },
    analysis:{
      bank: {money:0, tobo_paid:0,point:0,icp_award:0},
      num: {fans:0, store:0, vip: 0}
    },
    isp:{},
    vip:[]
  },
  onLoad: function (options){
    app.getParam(options);
  },
  onShow: function () {
    app.loginState(()=>{
      this.getApi();
    },true)
  },
  //获取会员数据
  getApi:function () {
    //读取服务商会员
    app.api().Get("api/v1/ais/vip/isp",(rel) => {
      if(200 == rel.code){
        this.setData({
          isp:rel.data
        })
      }
    })
    //读取服务商会员以外的会员列表
    app.api().Get("api/v1/ais/vip/index",(rel) => {
      if(200 == rel.code){
        this.setData({
          vip:rel.data
        })
      }
    })
    //读取会员开通信息
    app.api().Get("api/v1/ais/user/vip",(rel) => {
      if(200 == rel.code){
        this.setData({
          info:rel.data
        })
        if(rel.data.is_vip == 1 && rel.data.info.is_auto_up == 0){
          this.getAnalysis();
        }
      }
    })
  },
  //获取会员数据
  getAnalysis:function () {
    app.api().Get("api/v1/ais/user/analysis",(rel) => {
      if(200 == rel.code){
        this.setData({
          analysis:rel.data
        }) 
      }
		})
  },
  //开通会员
  onUrl:function (e) {
    if(this.data.loginstate){
      wx.navigateTo({
        url:e.currentTarget.dataset.url
      }) 
    }else{
      wx.navigateTo({
        url:'/packageHelper/pages/login',
      })
    }
  },
  //支付开通
  doPayment:function (e) {
    wx.showLoading({title:'加载中',mask: true });
    app.api().Post('api/v1/ais/vip/regVip',{vip_id: this.data.isp.id},(rel) => {
      wx.hideLoading();
      app.doWechatPay(rel.data,(res)=>{
        this.getApi();
      },(res) => {
        app.wxAlert('取消支付或开通失败')
      });
    })
  },
  //滚动
  onPageScroll(res) {
    let scrollTop = res.scrollTop;
    this.setData({
      showbar:220 >= scrollTop ? true : false
    })
  },
  //转发
  onShareAppMessage: function (e) {
    var dataset = e.from == "button"?e.target.dataset.detail:'isp'
    if(dataset == 'vip'){
      return {
        path: "/packageIndex/pages/vip/red?ucode="+app.globalData.loginuser.ucode
      }
    }else if(dataset == 'store'){
      return {
        path: "/packageAdmin/pages/store/regstore?ucode="+app.globalData.loginuser.ucode
      }
    }else{
      return {
        path: "/packageIndex/pages/vip/isp?ucode="+app.globalData.loginuser.ucode
      }
    }
  },
  //邀请码
  onQrcode: function (e) {
    this.setData({
      showQrcode:!this.data.showQrcode
    });
    if(this.data.showQrcode){
      this.getCode();
    }
  },
  //生成二维码
  getCode: function () {
    let that = this;
    wx.showLoading({
      title: '正在生成邀请码',
    })
    app.api().Post('openapi/v1/user/MiniProgramQrCode', {
      scene: {ucode:app.globalData.loginuser.ucode},
      page: 'packageIndex/pages/vip/isp',
      name: 'ais_isp_'+app.globalData.loginuser.ucode
    }, function (result) {
      that.setData({
        qrcode: result.data,
      })
      wx.hideLoading();
    })
  },
})